﻿package src
{
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	import src.PerPixColDec;
	import flash.display.Sprite;
	
	
	public class Game extends MovieClip
	{
		private var _movingUp:Boolean;
		private var _movingDown:Boolean;
		private var _movingRight:Boolean;
		private var _movingLeft:Boolean;
		
		public var mask_expanding:Boolean;
		public var t:uint=0;
		
		private var flyArray:Array;
		private var fly:AFly;
		public var fliesEaten:uint;
		private var difficulty:Number;
		
		private var par:Bat;
		private var isPaused:Boolean;
		
		private var pauseSplash:Pause;
		
		public function Game(myParent:Bat)
		{
			addEventListener(Event.ADDED_TO_STAGE,init);
			par=myParent;
			this.cacheAsBitmap=true;
			myMask.cacheAsBitmap=true;
			
		}
		
		private function init(e:Event){
			removeEventListener(Event.ADDED_TO_STAGE,init);
			this.mask=myMask;
			
			_movingUp=false;
			_movingDown=false;
			_movingLeft=false;
			_movingRight=false;
			
			mask_expanding=false;
			addEventListener(Event.ENTER_FRAME,onEnterFrame);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, onPress);
			stage.addEventListener(KeyboardEvent.KEY_UP, onRelease);
			myMask.scaleX=5;myMask.scaleY=5;
			
			flyArray= new Array();
			fliesEaten=0;
			
			fly=new AFly();
			flyContainer.addChild(fly);
			fly.x = 350; fly.y = 350;
			/*fly.x=Math.random()*flyContainer.width;
			fly.y=Math.random()*flyContainer.height;*/
			flyArray.push(fly);
					
		}
		
		private function onEnterFrame(e:Event):void{
						
			movementHandler();
			maskHandler();
			fliesMovementHandler();
						
			par.hud.score.text="Puntaje: " + fliesEaten.toString();
			if (par.game_files.data.bestScore<=fliesEaten){par.game_files.data.bestScore=fliesEaten}
			
		}
		
		private function maskHandler(){
			if (mask_expanding){
				if (myMask.scaleX<=10 && myMask.scaleX>0){myMask.scaleX += 0.1; myMask.scaleY += 0.1}
			} else {
				if (myMask.scaleX>0){
					myMask.scaleX -= 0.0065 + difficulty;
					myMask.scaleY -= 0.0065 + difficulty}
			}
			t++;
			
			if (t==5){mask_expanding=false}
			
			if (myMask.scaleX<=0.45){
				removeEventListener(Event.ENTER_FRAME,onEnterFrame);
				stage.removeEventListener(KeyboardEvent.KEY_DOWN, onPress);
				stage.removeEventListener(KeyboardEvent.KEY_UP, onRelease);
				par.displayMenu("Results")}
		}
		
		private function onPress(e:KeyboardEvent){
			switch( e.keyCode )
			{
				case Keyboard.UP:
					_movingUp = true;
					break;
				case Keyboard.DOWN:
					_movingDown = true;
					break;
				case Keyboard.LEFT:
					_movingLeft = true;
					break;
				case Keyboard.RIGHT:
					_movingRight = true;
					break;
				case Keyboard.P:
					pauseHandler();
				break;
			}
		}
		
		private function onRelease(e:KeyboardEvent){
			switch( e.keyCode )
			{
				case Keyboard.UP:
					_movingUp = false;
					break;
				case Keyboard.DOWN:
					_movingDown = false;
					break;
				case Keyboard.LEFT:
					_movingLeft = false;
					break;
				case Keyboard.RIGHT:
					_movingRight = false;
					break;
			}
		}
		
		private function movementHandler(){
			difficulty=fliesEaten*0.0001;
			
			if (_movingUp && colDecUp.hitTestObject(flySpace)){bg.y += 15; border.y += 15; 
				flySpace.y += 15, flyContainer.y += 15}
			if (_movingDown && colDecDown.hitTestObject(flySpace)){bg.y -= 15; border.y -= 15;
				flySpace.y -= 15, flyContainer.y -= 15}
			if (_movingLeft && colDecLeft.hitTestObject(flySpace)){bg.x += 15; border.x += 15;
				flySpace.x += 15, flyContainer.x += 15, player.scaleX= -1}
			if (_movingRight && colDecRight.hitTestObject(flySpace)){bg.x -= 15; border.x -= 15;
				flySpace.x -= 15, flyContainer.x -= 15, player.scaleX= 1}
		}
		
		private function fliesMovementHandler(){
			for (var i:uint=0; i<flyArray.length; i++){
				var dx:Number = flyContainer.x+flyArray[i].x;
				var dy:Number = -(flyContainer.y+flyArray[i].y);
				var distance:Number = Math.sqrt(Math.pow(dx,2)+Math.pow(dy,2));
				var angle:Number;
				if (distance<650){
					angle = (Math.atan2(dy,dx)*180/Math.PI-5/Math.PI)+(Math.random()*10/Math.PI);
					flyArray[i].x+=4500*Math.cos(angle)/distance;
					flyArray[i].y+=4500*Math.sin(angle)/distance;
					/*trace(distance)
					trace(i+" close angle :"+angle+"°")*/
				} else {
					angle = Math.random()*180/Math.PI;
					flyArray[i].x+=10*Math.cos(angle);
					flyArray[i].y+=10*Math.sin(angle);
						//trace(i+" far angle :"+angle+"°")
				}
				if (!flyArray[i].hitTestObject(myMask)&&!flyArray[i].hitTestObject(flySpace)){
						flyArray[i].x=Math.random()*flyContainer.width;
						flyArray[i].y=Math.random()*flyContainer.height;}
				var collides:Boolean;
				collides = PerPixColDec.isColliding(flyArray[i], player, flySpace, true, 0);
				if (collides && myMask.scaleX>=0.9){
					//trace(i+" collided")
					flyContainer.removeChild(flyArray[i]);
					flyArray.splice(i,1);
					fliesEaten++;
					if (flyArray.length<2){
						var rng_god:uint;
						rng_god=Math.round(Math.random()*2);
						if (rng_god==0){rng_god=1}
						for (var j:uint=0; j<rng_god; j++){
							fly=new AFly();
							flyContainer.addChild(fly);
							fly.x=Math.random()*flyContainer.width;
							fly.y=Math.random()*flyContainer.height;
							flyArray.push(fly)}
					}
				}					
			}
		}
		
		private function pauseHandler(){
			isPaused=!isPaused;
			if (isPaused){
				removeEventListener(Event.ENTER_FRAME,onEnterFrame);
				pauseSplash=new Pause();
				addChild(pauseSplash);
				par.hud.active=false;
				player.stop();
			}else{
				addEventListener(Event.ENTER_FRAME,onEnterFrame);
				removeChild(pauseSplash);
				par.hud.active=true;
				player.play();
			}
		}
		
	}
}